<template>
  <div >
    <el-dialog title="创建可追踪URL" :visible.sync="dialogVisible">
      <el-form ref="form" :model="form" label-width="80px" v-loading="loading">
        <el-form-item label="渠道来源">
          <el-select v-model="form.cid" placeholder="请选择活动区域">
            <el-option v-for="option in options" :label="option.channel" :value="option.cid"
                       :key="option.cid"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input type="textarea" v-model="form.remarks"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button :loading="saving" type="primary" @click="handleMakesure">确定</el-button>
      </span>
    </el-dialog>
  </div>


</template>

<script type="text/babel">
  import { getAllChannel, addTraceableUrl } from '@/service/getData'

export default {
    data () {
      return {
        loading: false,
        saving: false,
        msg: 'create url',
        dialogVisible: false,
        options: [],
        form: {
          cid: '',
          remarks: ''
        }
      }
    },
    props: ['shareUrl'],
    methods: {
      axiosGetAllChannel () {
        this.loading = true
        getAllChannel()
          .then((res) => {
            if (res.data && 'code' in res.data && res.data.code === 1) {
              var resJson = res.data
              this.options = resJson.data.list
            }
          })
          .finally(() => {
            this.loading = false
          })
      },
      axiosAddTraceableUrl (payload) {
        addTraceableUrl(payload)
          .then((res) => {
            if (res.data && 'code' in res.data && res.data.code === 1) {
              var resJson = res.data
              this.$message({
                type: 'info',
                message: resJson.msgZ || resJson.msgE
              })
              this.$emit('axiosSuccess')
            }
          })
          .finally(() => {
            this.loading = false
          })
      },
      handleMakesure () {
        if (!this.form.cid) {
          this.$message({
            type: 'warning',
            message: '请选择渠道来源'
          })
          return null
        }
        this.saving = true
        this.axiosAddTraceableUrl({
          'logicId': parseInt(this.$route.params.id),   // 微页面ID
          'channelId': parseInt(this.form.cid),  // 渠道来源
          'remarks': this.form.remarks, // 备注
          'shareUrl': this.shareUrl
        }).then(() => {
          this.saving = false
        })
      },
      init () {
        this.axiosGetAllChannel()
        this.dialogVisible = true
      },
      reset () {
        this.form = {
          cid: '',
          remarks: ''
        }
      }
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss" rel="stylesheet/scss">
  .dialog {
    .el-form {
      .el-form-item:last-child {
        margin-bottom: 0;
      }
    }
  }
</style>
